$(function () {
    //渲染用户名和头像
    getUserInfo()

    // 退出
    $('.logout a').on('click',function () {
        layer.confirm('确认退出登录吗？', {icon: 3, title:'提示'}, function(index){
            //do something
            localStorage.removeItem('token')
            location.href = '/login.html'
            layer.close(index);
          })
    })
})


function getUserInfo() {
    axios({
        method:'GET',
        url:'/my/userinfo',
        // headers:{
        //     Authorization:localStorage.getItem('token')
        // }
    }).then(res => {
        if (res.data.status !== 0) {
            return layui.layer.msg(res.data.message)
        }
        // console.log(res.data)

        renderAvatar(res.data.data)
    })
}

function renderAvatar(user) {
    let name = user.nickname || user.username

    // 渲染名称
    $('.welcome').html('欢迎&nbsp'+ name)
    
    // 渲染头像
    if (user.user_pic !== null) {
        $('.layui-nav-img').show().attr('src',user.user_pic)
        $('.avatar-text').hide()
    }else{
        $('.layui-nav-img').hide()
        let first = name[0].toUpperCase()
        $('.avatar-text').show().html(first)
    }
}